package com.example.musiccommunity.UserPage;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.musiccommunity.MainActivity;
import com.example.musiccommunity.NoticeDetail;
import com.example.musiccommunity.R;
import com.example.musiccommunity.bean.Notice;
import com.example.musiccommunity.util.GridAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;

public class NoticeList extends AppCompatActivity {
    private Toolbar toolbar;
    private EditText etQuery;
    private ImageView search;
    private GridView grid;
    BaseAdapter mAdapter = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_list);

        toolbar = findViewById(R.id.toolbar);
        etQuery = findViewById(R.id.et_query);
        search = findViewById(R.id.search);
        grid = findViewById(R.id.grid);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //软键盘搜索
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=etQuery.getText().toString();
                reqString(str);
            }
        });

        reqString("");

    }
    void reqString(String key){
        String url= MainActivity.ip+"/notice/get?key="+key;
        System.out.println(url);
        StringRequest stringRequest=new StringRequest(Request.Method.POST,url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                //s = new String(s.getBytes("ISO-8859-1"), "UTF-8");
                Log.d("user***********user",s.toString());
                if(s==null || s.equals("")){
                    Toast.makeText(getBaseContext(), "获取数据失败", Toast.LENGTH_SHORT).show();
                }
                else{
                    ArrayList<Notice> noticeArrayList =new ArrayList<>();
                    JSONObject jsonObject = null;
                    try {
                        JSONArray jsonArray = new JSONArray(s);
                        for (int i=0;i<jsonArray.length();i++){
                            jsonObject = jsonArray.getJSONObject(i);
                            String id = jsonObject.get("id").toString();
                            Notice n=new Notice(id,
                                    jsonObject.get("title").toString(),
                                    jsonObject.get("txt").toString(),
                                    jsonObject.get("time").toString() );
                            noticeArrayList.add(n);
                        }
                        Collections.reverse(noticeArrayList);
                        mAdapter = new GridAdapter<Notice>(noticeArrayList, R.layout.list_notice) {
                            @Override
                            public void bindView(ViewHolder holder, Notice obj) {
                                holder.setText(R.id.title, obj.getTitle());
                                holder.setText(R.id.time, obj.getTime());
                            }

                        };
                        grid.setAdapter(mAdapter);
                        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent();
                                intent.putExtra("id",noticeArrayList.get(i).getId());
                                intent.setClass(getBaseContext() , NoticeDetail.class);
                                startActivity(intent);
                            }
                        });

                    } catch (JSONException e) {
                        System.out.println(e.toString());
                        throw new RuntimeException(e);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("--------",volleyError.toString());
                Toast.makeText(getBaseContext(), "获取数据失败", Toast.LENGTH_SHORT).show();
            }
        });//字符串
        MainActivity.requestQueue.add(stringRequest);

    }




}